package com.hgb.mall.portal.services.impl;

import com.github.pagehelper.PageHelper;
import com.hgb.mall.common.exception.ApiException;
import com.hgb.mall.mbg.entity.OmsOrderItem;
import com.hgb.mall.mbg.entity.PmsComment;
import com.hgb.mall.mbg.entity.UmsMember;
import com.hgb.mall.mbg.mapper.OmsOrderItemDynamicSqlSupport;
import com.hgb.mall.mbg.mapper.OmsOrderItemMapper;
import com.hgb.mall.mbg.mapper.PmsCommentDynamicSqlSupport;
import com.hgb.mall.mbg.mapper.PmsCommentMapper;
import com.hgb.mall.portal.domain.CommentParam;
import com.hgb.mall.portal.services.CommentService;
import com.hgb.mall.portal.services.UmsMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Optional;

import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    PmsCommentMapper commentMapper;
    @Autowired
    OmsOrderItemMapper orderItemMapper;
    @Autowired
    UmsMemberService memberService;

    @Override
    public List<PmsComment> list(Long productId,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return commentMapper.select(c->c
                .where(PmsCommentDynamicSqlSupport.productId,isEqualTo(productId))
                .and(PmsCommentDynamicSqlSupport.showStatus,isEqualTo(1))
                .orderBy(PmsCommentDynamicSqlSupport.id.descending())
        );
    }

    @Override
    @Transactional
    public int add(CommentParam commentParam) {
        OmsOrderItem orderItem = orderItemMapper
                .selectByPrimaryKey(commentParam.getOrderItemId()).orElseThrow(()->new ApiException("商品不存在"));
        UmsMember member = memberService.getCurrentMember();
        PmsComment pmsComment = new PmsComment();
        pmsComment.setCreateTime(new Date());
        pmsComment.setContent(commentParam.getContent());
        pmsComment.setMemberIcon(member.getIcon());
        pmsComment.setMemberNickName(member.getNickname());
        pmsComment.setPics(commentParam.getPics());
        pmsComment.setProductAttribute(orderItem.getProductAttr());
        pmsComment.setProductName(orderItem.getProductName());
        pmsComment.setCollectCouont(0);
        pmsComment.setReadCount(0);
        pmsComment.setProductId(orderItem.getProductId());
        pmsComment.setReplayCount(0);
        pmsComment.setShowStatus(1);
        pmsComment.setStar(commentParam.getStar());
        pmsComment.setMemberNickName(Optional.ofNullable(member.getNickname()).orElse(member.getUsername()));
        orderItemMapper.update(c->c
                .set(OmsOrderItemDynamicSqlSupport.commentStatus)
                .equalTo(1)
                .where(OmsOrderItemDynamicSqlSupport.id,isEqualTo(commentParam.getOrderItemId()))
        );
        return commentMapper.insertSelective(pmsComment);
    }
}
